import {addRecObject, queryRecObject, delRecObject, modRecObject} from '../services/operation'
export default {
    namespace: "recObjectEdit",
    state: {
        data: {
            list: [],
            pagination: {},
        },
        recObjectList: [],
        loading: false,
    },

    effects: {
        // * fetch({payload}, {call, put}){
        //     yield put({
        //         type: 'changeLoading',
        //         payload: true,
        //     });
        //     const response = yield call(queryRecObject, payload);
        //     yield put({
        //         type: 'save',
        //         payload: response,
        //     });
        //     yield put({
        //         type: 'changeLoading',
        //         payload: false,
        //     });
        // },

        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(addRecObject, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) callback(response);
        },

        * mod({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(modRecObject, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },

        // * del({payload, callback}, {call, put}){
        //     yield put({
        //         type: 'changeLoading',
        //         payload: true,
        //     });
        //     const response = yield call(delRecObject, payload);
        //     yield put({
        //         type: 'changeLoading',
        //         payload: false,
        //     });
        //     if (callback) callback(response);
        // },
    },

    reducers: {
        save(state, action){
            const recObjectList = [];
            getRecObjectList(recObjectList, action.payload.resultData.recObjectList.list);
            function getRecObjectList(tmpR, list) {
                for (var i = 0; i < list.length; i++){
                    const recObjectItem = {value: list[i]["priority_id"], label: list[i]["status"]};
                    if (list[i]["children"]){
                        recObjectItem["children"] = [];
                        getRecObjectList(recObjectItem["children"], list[i]["children"]);
                    }
                    tmpR.push(recObjectItem);
                }
            }
            return{
                ...state,
                recObjectList: recObjectList,
                data: action.payload.resultData.recObjectList,
            };
        },

        changeLoading(state, action){
            return{
                ...state,
                loading: action.payload,
            };
        },
    },
}